const express = require('express');
const router = express.Router();
const retUtil = require('../util/retUtil');
const activityRouterUtil = require('./routerUtil/activityRouterUtil');
const activityService = require('../../service/activityService');

router.get('/',function (req, res, next) {
    res.send('respond with a 201 resource');
});

router.use(function (req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    next();
});


/*
* 202-获取活动列表
*
* @author 王俊
* @date 2018-07-30
*
* 参数: device
* 参数: states
* 参数: pageNum
* 参数: pageSize
* 参数: withTotalPage
*
* */
function getActivityList(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const states = retUtil.arrayFromReq(req, 'states');
    const pageNum = retUtil.numberFromReq(req, 'pageNum');
    const pageSize = retUtil.numberFromReq(req, 'pageSize');
    const withTotalPage = retUtil.numberFromReq(req, 'withTotalPage');

    if(!states || states.length <=0) {
        return retUtil.ret_err_json('states 错误', res);
    }
    if (!pageSize || pageSize > 100) {
        return retUtil.ret_err_json('pageSize 错误或过大', res);
    }

    const opt = {
        device: device,
        type: 20,   // 标记图片活动
        states: states,
        pageNum: pageNum,
        pageSize: pageSize,
        withTotalPage: withTotalPage,
        publish: 1,     // 获取已发布的活动
    };
    activityService.getActivityList(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getActivityList', getActivityList);
router.post('/getActivityList', getActivityList);

/*
* 202-获取活动详情
*
* @author 王俊
* @date 2018-07-30
*
* 参数: device
* 参数[可选]: userId
* 参数: actId
* 参数: participant
* 参数: participantLimit
*
* */
function getActivityDetail(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const actId = retUtil.numberFromReq(req, 'actId');
    const participantLimit = retUtil.numberFromReq(req, 'participantLimit') || 12;

    if(!actId) {
        return retUtil.ret_err_json('actId 错误', res);
    }

    const opt ={
        device: device,
        actId: actId,
        type: 20,   // 标记图片活动
        userId: userId,
        participantLimit,
    };

    activityRouterUtil.getImagePraiseActivityDetail(opt, function (error, data) {
        if(error) return retUtil.ret_err_json(error, res);
        return retUtil.ret_succ_json(data, res)
    });
}
router.get('/getActivityDetail', getActivityDetail);
router.post('/getActivityDetail', getActivityDetail);


/*
* 202-提交作品
*
* @author 王俊
* @date 2018-07-30
*
* 参数: device
* 参数: userId
* 参数: actId
* 参数: url
*
* */
function submitProduct(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const actId = retUtil.numberFromReq(req, 'actId');
    const url = retUtil.stringFromReq(req, 'url');
    if(!url || url.indexOf('http') !== 0) {
        return retUtil.ret_err_json('url 错误', res);
    }
    if(!userId) {
        return retUtil.ret_err_json('userId 错误', res);
    }

    const opt = {
        device : device,
        type: 20,           // 图片点赞活动
        userId: userId,
        url: url,
        activityId: actId
    };
    activityService.submitProductToAct(opt, function (error, data) {
        if(error) return retUtil.ret_err_json(error, res);
        return retUtil.ret_succ_json(data, res);
    })
}
router.get('/submitProduct', submitProduct);
router.post('/submitProduct', submitProduct);


/*
* 202-获取作品详情
*
* @author 王俊
* @date 2018-07-30
*
* 参数: device
* 参数: productId
* 参数: userId
* 参数: likeRewardType
*
* */
function getProductDetail(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const productId = retUtil.numberFromReq(req, 'productId');
    const userId = retUtil.numberFromReq(req, 'userId');
    const likeLimit = retUtil.numberFromReq(req, 'likeLimit');
    const likeRewardType = retUtil.numberFromReq(req, 'likeRewardType') || 31; // 31点赞方奖励, 32被点赞方奖励',

    if(!productId) {
        return retUtil.ret_err_json('productId 错误', res);
    }
    const opt = {
        device: device,
        productId: productId,
        userId: userId,
        likeLimit: likeLimit,
        likeRewardType: likeRewardType,
    };

    activityRouterUtil.getProductDetail(opt, function (error, data) {
        if (error) return retUtil.ret_err_json(error, res);
        return retUtil.ret_succ_json(data, res);
    });



}
router.get('/getProductDetail', getProductDetail);
router.post('/getProductDetail', getProductDetail);







/*
* 202-给作品点赞
*
* @author 毛凯
* @date 2018-07-30
*
* 参数: device
* 参数: userId
* 参数: productId
* 参数：actId
* */
function giveALikeToProduct(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const productId = retUtil.numberFromReq(req, 'productId');
    const actId = retUtil.numberFromReq(req, 'actId');

    if(!device || device < 700) {
        return retUtil.ret_err_json('device参数错误', res);
    }
    if (!userId || userId <= 0 || !productId || productId <= 0 || !actId || actId <= 0) {
        return retUtil.ret_err_json('参数错误', res);
    }

    const opt = {
        device: device,
        userId: userId,
        productId: productId,
        actId : actId
    };
    activityService.giveALikeToProduct(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/giveALikeToProduct', giveALikeToProduct);
router.post('/giveALikeToProduct', giveALikeToProduct);



/*
* 202-获取活动随机奖励
*
* @author 毛凯
* @date 2018-07-30
*
* 参数: device
* 参数: userId
* 参数：actId
* */
function getActivityRandomReward(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const actId = retUtil.numberFromReq(req, 'actId');

    if(!device || device < 700) {
        return retUtil.ret_err_json('device参数错误', res);
    }
    if (!userId || userId <= 0 || !actId || actId <= 0) {
        return retUtil.ret_err_json('参数错误', res);
    }

    const opt = {
        device: device,
        userId: userId,
        actId : actId
    };
    activityService.getActivityRandomReward(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getActivityRandomReward', getActivityRandomReward);
router.post('/getActivityRandomReward', getActivityRandomReward);


//==========================================================================
module.exports = router;